package com.huobao.yunying.activitys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.Regions;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.SelectAreaPopupWindow;
import com.huobao.zhangying.R;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Act_Edit_Msg extends HuobaoBaseActivity implements OnClickListener {
	private ImageView img_userphoto;
	private TextView tv_name;
	private TextView tv_username;
	private EditText edt_truename;
	private EditText edt_storename;
	private TextView tv_tel;
	private EditText edt_dianhua;
	private EditText edt_qqnum;
	private TextView tv_address;
	private TextView tv_guojia;
	private TextView tv_sheng;
	private TextView tv_shi;
	private EditText edt_address;
	@ViewInject(R.id.llyt_true_address)
	private View llyt_true_address;
	@ViewInject(R.id.llyt_edit_address)
	private View llyt_edit_address;
	@ViewInject(R.id.btn_edit)
	private Button btn_edit;

	private boolean isShop = false;

	private List<Regions> m_AllRegions = new ArrayList<Regions>();

	protected void show(JSONObject jsonObject) {
		if (isShop)
			ImageUtils.loadimg(img_userphoto, getIntent().getStringExtra("img"));
		else
			ImageUtils.loadimg(img_userphoto, jsonObject.getString("portrait"));
		setTextValue(tv_name, jsonObject, "user_name");
		setTextValue(tv_username, jsonObject, "user_name");
		setTextValue(edt_truename, jsonObject, "real_name");
		setTextValue(edt_storename, jsonObject, "shop_name");
		setTextValue(tv_tel, jsonObject, "phone_mob");
		setTextValue(edt_dianhua, jsonObject, "phone_tel");
		setTextValue(edt_qqnum, jsonObject, "im_qq");
		setTextValue(tv_address, jsonObject, "region_name");
		setTextValue(edt_address, jsonObject, "maddress");
		try {
			this.region_id = jsonObject.getString("region_id");
			this.region_name = jsonObject.getString("region_name");
		} catch (Exception e) {
		}

	}

	private void setTextValue(TextView textView, JSONObject jsonObject, String key) {
		String value = null;
		try {
			value = jsonObject.getString(key);
		} catch (Exception e) {
		}
		if (value == null || value.equals("") || value.equals("null"))
			textView.setText("");
		else
			textView.setText(value);
	}

	@Override
	protected int getContentView() {
		return R.layout.layout_edit_msg;
	}

	@Override
	protected String getTitleText() {
		return "我的名片";
	}

	@Override
	protected void init() {
		isShop = getIntent().getBooleanExtra("flag", false);
		initView();
		tv_sheng.setOnClickListener(this);
		tv_shi.setOnClickListener(this);
		btn_edit.setOnClickListener(this);
		getUserMsg();
		getRegions();
	}

	private void initView() {
		img_userphoto = (ImageView) findViewById(R.id.img_userphoto);
		tv_name = (TextView) findViewById(R.id.tv_name);
		tv_username = (TextView) findViewById(R.id.tv_username);
		edt_truename = (EditText) findViewById(R.id.edt_truename);
		edt_storename = (EditText) findViewById(R.id.edt_storename);
		tv_tel = (TextView) findViewById(R.id.tv_tel);
		edt_dianhua = (EditText) findViewById(R.id.edt_dianhua);
		edt_qqnum = (EditText) findViewById(R.id.edt_qqnum);
		tv_address = (TextView) findViewById(R.id.tv_address);
		tv_guojia = (TextView) findViewById(R.id.tv_guojia);
		tv_sheng = (TextView) findViewById(R.id.tv_sheng);
		tv_shi = (TextView) findViewById(R.id.tv_shi);
		edt_address = (EditText) findViewById(R.id.edt_address);
	}

	private void getUserMsg() {
		Map<String, String> map;
		if (isShop == false) {
			map = HttpUtil.getParams("app", "member", "act", "profile");
		} else {
			map = HttpUtil.getParams("app", "shopmember", "act", "shopprofile");
			getOtherMsg();
		}

		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {

				JSONObject jsonObject = JSON.parseObject(result);
				if (isShop) {
					JSONArray array = jsonObject.getJSONArray("retval");
					jsonObject = array.getJSONObject(0);
				} else {
					jsonObject = jsonObject.getJSONObject("retval");
				}

				show(jsonObject);
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}

	private void getOtherMsg() {
		Map<String,String> map = HttpUtil.getParams("app","shopmember","act","store_shezhi");
		Http.getInstant().sendRequest(this, map, new HttpListener() {
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				jsonObject = jsonObject.getJSONObject("store");
				String storename = jsonObject.getString("store_name");
				edt_storename.setText(storename);
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}

	private String region_name, region_id;

	private String m_Select_Sheng_name;
	private List<Regions> m_Selects_Shi;

	@Override
	public void onClick(View v) {
		final SelectAreaPopupWindow popupWindow = new SelectAreaPopupWindow();
		switch (v.getId()) {
		case R.id.tv_guojia:
			break;
		case R.id.tv_sheng:
			tv_shi.setVisibility(View.GONE);
			popupWindow.showPop(context, m_AllRegions, v, new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					popupWindow.dismiss();
					m_Selects_Shi = m_AllRegions.get(position).children;
					m_Select_Sheng_name = m_AllRegions.get(position).region_name;
					tv_sheng.setText(m_AllRegions.get(position).region_name);
					if (m_Selects_Shi != null && m_Selects_Shi.size() != 0) {
						tv_shi.setVisibility(View.VISIBLE);
						tv_shi.setText("请选择...");
					} else {
						tv_shi.setVisibility(View.GONE);
						region_id = m_AllRegions.get(position).region_id;
						region_name = "中国 " + m_Select_Sheng_name;
					}

				}
			});
			break;
		case R.id.tv_shi:
			popupWindow.showPop(context, m_Selects_Shi, v, new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					popupWindow.dismiss();
					region_id = m_Selects_Shi.get(position).region_id;
					region_name = "中国 " + m_Select_Sheng_name + m_Selects_Shi.get(position).region_name;
					tv_shi.setText(m_Selects_Shi.get(position).region_name);
				}
			});
			break;
		case R.id.btn_edit:
			// 编辑编辑地址
			llyt_edit_address.setVisibility(View.VISIBLE);
			llyt_true_address.setVisibility(View.GONE);
			tv_sheng.setVisibility(View.VISIBLE);
			tv_sheng.setText("请选择...");
			break;
		}
	}

	public void onSaveCLick(View view) {
		String real_name = edt_truename.getText().toString().trim();
		String shop_name = edt_storename.getText().toString().trim();
		String phone_tel = edt_dianhua.getText().toString().trim();
		String im_qq = edt_qqnum.getText().toString().trim();
		String address = edt_address.getText().toString().trim();
		Map<String, String> map;
		if(isShop){
			map = HttpUtil.getParams("app", "shopmember", "act", "profile", "real_name", real_name, "shop_name",
					shop_name, "phone_tel", phone_tel, "im_qq", im_qq, "address", address, "region_name", region_name,
					"region_id", region_id);
		}else{
			if (region_name == null) {
				map = HttpUtil.getParams("app", "member", "act", "editprofile", "real_name", real_name, "shop_name",
						shop_name, "phone_tel", phone_tel, "im_qq", im_qq, "address", address);
			} else {
				map = HttpUtil.getParams("app", "member", "act", "editprofile", "real_name", real_name, "shop_name",
						shop_name, "phone_tel", phone_tel, "im_qq", im_qq, "address", address, "region_name", region_name,
						"region_id", region_id);
			}
		}

		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				Toast.makeText(context, jsonObject.getString("retval"), Toast.LENGTH_SHORT).show();
				finish();
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}

	private void getRegions() {
		Map<String, String> map = HttpUtil.getParams("app", "regions");
		Http.getInstant().sendRequest(context, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				JSONArray array = jsonObject.getJSONArray("retval");
				for (int i = 0; i < array.size(); i++) {
					JSONObject object = array.getJSONObject(i);
					Regions regions = new Regions();
					regions.region_id = object.getString("region_id");
					regions.region_name = object.getString("region_name");
					regions.children = JSON.parseArray(object.getString("children"), Regions.class);
					m_AllRegions.add(regions);
				}
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}
}
